module game
{
	export class RegiterView extends eui.Component
	{
		private account_input:eui.TextInput;
		private password_input:eui.TextInput;
		private email_input:eui.TextInput;
		private login_btn:eui.Button;
		private loading_group:eui.Group;
		private loading_label:eui.Label;

		public constructor()
		{
			super();
			this.skinName = "resource/ui/Register.exml";
		}

		protected childrenCreated(): void
		{
			this.login_btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
			this.loading_group.visible = false;
			this.email_input.inputType
		}

		private onClick(e:egret.TouchEvent):void
		{
			if(this.account_input.text.length == 0 || this.password_input.text.length == 0)
			{
				return alert("请输入帐号和密码！");
			}
			
			if(RegExpUtil.checkEmail(this.email_input.text) == false)
			{
				return alert("邮箱错误！！");
			}

			if((this.account_input.text.length >= 6 && this.account_input.text.length <= 10) || (this.password_input.text.length >= 6 && this.password_input.text.length <= 10))
			{
				this.sendGetRequest(this.account_input.text,this.password_input.text,this.email_input.text);
				this.loading_group.visible = true;
			}
			else
			{
				return alert("帐号密码必须输入6--10位字符！");
			}
		}

		private sendGetRequest(acc:string,pas:string,email:string):void 
		{
			let base64 = com.Base64Util.StringToBase64(acc + ':' + pas + ':' + email);
			let request = new egret.HttpRequest();
			request.responseType = egret.HttpResponseType.TEXT;
			request.open(com.GlobalData.http_host + "register?credential=" + base64,egret.HttpMethod.GET);
			console.log("发送http请求....");
			request.send();
			request.addEventListener(egret.Event.COMPLETE,this.onGetComplete,this);
			request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError,this);
			request.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress,this);
		} 
		
		private onGetComplete(event:egret.Event):void
		{
			var request = <egret.HttpRequest>event.currentTarget;
        	console.log("get data : ",request.response);
			this.loading_group.visible = false;
			
		}

		private onGetIOError(event:egret.IOErrorEvent):void
		{
			console.log("get error : " + event);
			console.log(event);
		}

		private onGetProgress(event:egret.ProgressEvent):void
		{
			console.log("get progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
		}
	}
}